package com.undsf.despot.actors;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Arathi on 2016/10/5.
 */
public class Menu extends Group {
    protected MenuBody body;
    protected List<MenuItem> itemList;
    protected Color fgColor;
    protected Color bgColor;
    protected BitmapFont font12;

    public Menu() {
        body = new MenuBody("命令菜单", 112, 48);
        body.setColors(new Color(0x000000ee), Color.WHITE);

        itemList = new ArrayList<MenuItem>();

        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/simsun.ttc"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        parameter.size = 12;
        StringBuffer sb = new StringBuffer();
        sb.append(FreeTypeFontGenerator.DEFAULT_CHARS);
        for (char c=0x4E00; c<=0x9FA5; c++) {
            sb.append(c);
        }
        parameter.characters = sb.toString();
        font12 = generator.generateFont(parameter); // font size 12 pixels
        generator.dispose(); // don't forget to dispose to avoid memory leaks!

        this.addActor(body);
    }

    @Override
    public void setPosition(float x, float y) {
        super.setPosition(x, y);
        for (Actor actor : getChildren()){
            actor.setPosition(x, y);
        }
    }

    public void addItem(String caption, String command) {
        // itemList.add(new MenuItem(caption, command));
        addItem(caption, command, 0, 0);
    }

    public void addItem(String caption, String command, int cellX, int cellY) {
        addActor(new MenuItem(caption, command, this, cellX, cellY));
    }

    // public void drawItems(Batch batch) {
    //     int index = 0;
    //     int startX = (int)getX();
    //     int startY = (int)getY();
    //     for (MenuItem item : itemList) {
    //         font12.setColor(fgColor);
    //         font12.draw(batch, item.caption, startX, startY-16*index);
    //         index++;
    //     }
    // }
}

class MenuBody extends Actor {
    // public static final int TILE_SIZE = 16;
    protected String title;
    protected ShapeRenderer renderer;
    protected Color fgColor;
    protected Color bgColor;
    protected int width;
    protected int height;

    public MenuBody() {
        init(null, 112, 48, Color.BLACK, Color.WHITE);
    }

    @Deprecated
    public MenuBody(int width, int height) {
        init(null, width, height, Color.BLACK, Color.WHITE);
    }

    public MenuBody(String title, int width, int height) {
        init(title, width, height, Color.BLACK, Color.WHITE);
    }

    public void init(String title, int width, int height, Color bgColor, Color fgColor) {
        renderer = new ShapeRenderer();
        this.title = title;
        this.width = width;
        this.height = height;
        this.bgColor = bgColor;
        this.fgColor = fgColor;
    }

    public void setColors(Color bgColor, Color fgColor) {
        this.bgColor = bgColor;
        this.fgColor = fgColor;
    }


    private void drawRect(float x0, float y0, float width, float height, Color color, boolean fill) {
        ShapeRenderer.ShapeType sharpType = fill ? ShapeRenderer.ShapeType.Filled : ShapeRenderer.ShapeType.Line;
        renderer.setColor(color);
        renderer.begin(sharpType);
        if (fill) {
            renderer.rect(x0, y0, width, height);
        }
        else {
            renderer.rect(x0+1, y0+1, width-1, height-1);
        }
        renderer.end();
        if (!fill) {
            renderer.begin(ShapeRenderer.ShapeType.Point);
            renderer.point(x0, y0, 0);
            renderer.end();
        }
    }

    public void drawBackground() {
        drawRect(getX(), getY(), width, height, bgColor, true);
    }

    public void drawBorder() {
        drawRect(getX()+1, getY()+2, width-2, height-4, fgColor, false);
        drawRect(getX()+2, getY()+1, width-4, height-2, fgColor, false);
        renderer.begin(ShapeRenderer.ShapeType.Point);
        renderer.setColor(Color.RED);
        renderer.point(getX()+3, getY()+3, 0);
        renderer.point(getX()+3, getY()+height-4, 0);
        renderer.point(getX()+width-4, getY()+3, 0);
        renderer.point(getX()+width-4, getY()+height-4, 0);
        renderer.end();
    }

    public void drawPointer() {
        //
    }

    public void drawTest() {
        // renderer.begin(ShapeRenderer.ShapeType.Line);
        // renderer.setColor(Color.RED);
        // //renderer.point(1,1,0);
        // renderer.setColor(Color.GREEN);
        // renderer.line(1,1,1,100);
        // renderer.end();
        renderer.begin(ShapeRenderer.ShapeType.Point);
        renderer.point(1,1,0);
        renderer.end();

        drawRect(2,2,100,100,new Color(0,0,0,0.5f),true);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        batch.end();

        renderer.setAutoShapeType(true);
        Gdx.gl.glEnable(GL20.GL_BLEND);
        drawBackground();
        drawBorder();
        // renderer.setAutoShapeType(false);

        // drawTest();
        batch.begin();
    }

    @Override
    public boolean remove() {
        renderer.dispose();
        return super.remove();
    }
}

class MenuItem extends Actor {
    public String caption;
    public String command;
    public Menu menu;
    public int cellX;
    public int cellY;

    @Deprecated
    public MenuItem(String caption, String command, Menu menu) {
        this.caption = caption;
        this.command = command;
        this.menu = menu;
    }

    public MenuItem(String caption, String command, Menu menu, int cellX, int cellY) {
        this.caption = caption;
        this.command = command;
        this.menu = menu;
        this.cellX = cellX;
        this.cellY = cellY;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        BitmapFont font = menu.font12;
        font.draw(batch, caption, 0, cellY * 16);
    }
}
